package com.ayg.service;

import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;

/**
 * 邮件发送的实现类
 */
@Service
public class MailService {
    private final static Logger logger = LoggerFactory.getLogger(MailService.class);

    @Value("${spring.mail.username}")
    private String mailUsername; //获取发件人的邮箱
    @Autowired
    private JavaMailSender javaMailSender;  //注入发邮件的对象
    @Autowired
    private TemplateEngine templateEngine;  //注入模板引擎

    /**
     * 激活账号邮件发送
     * @param activationUrl  激活url链接
     * @param email     收件人邮箱
     */
    public void sendMailForActivationAccount(String activationUrl,String email){
        //创建邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper message= new MimeMessageHelper(mimeMessage, true);
            //设置邮件主题
            message.setSubject("欢迎来到爱忆港 - 个人账号激活");
            //设置邮件发送者
            message.setFrom(mailUsername);
            //设置邮件接收人，可以多个
            message.setTo(email);
            //设置抄送人，可以多个
//            message.setCc();
            //设置隐秘抄送人，可以多个
//            message.setBcc();
            //设置邮件发送日期
            message.setSentDate(new Date());
            //创建上下文环境
            Context context = new Context();
            context.setVariable("activationUrl",activationUrl);
            String text = templateEngine.process("activation-account.html",context);
            //设置邮件正文
            message.setText(text, true);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        //邮件发送
        javaMailSender.send(mimeMessage);
    }

    /**
     * 密码重置邮件发送
     * @param activationPassUrl  密码重置url链接
     * @param userEmail     收件人邮箱
     */
    public void sendMailForActivationPassAccount(String activationPassUrl,String userEmail){
        //创建邮件对象
        MimeMessage mimeMessagePass = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper message= new MimeMessageHelper(mimeMessagePass, true);
            //设置邮件主题
            message.setSubject("欢迎来到爱忆港 - 个人密码重置");
            //设置邮件发送者
            message.setFrom(mailUsername);
            //设置邮件接收人，可以多个
            message.setTo(userEmail);
            //设置抄送人，可以多个
//            message.setCc();
            //设置隐秘抄送人，可以多个
//            message.setBcc();
            //设置邮件发送日期
            message.setSentDate(new Date());
            //创建上下文环境
            Context contextPass = new Context();
            contextPass.setVariable("activationPassUrl",activationPassUrl);
            String textPass = templateEngine.process("activationPass-account.html",contextPass);
            //设置邮件正文
            message.setText(textPass, true);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        //邮件发送
        javaMailSender.send(mimeMessagePass);
    }



}
